<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('移动策略')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="400px" @close='close'>
    <el-form ref="form" :model="record" label-width="120px" :rules="rules">
      <el-form-item :label="$t('需要移动的策略')" prop="index">
        <el-select v-model="record.index" ref="index" placeholder="" size='small' disabled>
          <el-option v-for="item in dataList" :key="item.index" :label="item.name" :value="item.index">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('移动至策略')" prop="dist_id">
        <el-select v-model="record.dist_id" ref="distId" :placeholder="$t('请选择')" size='small' filterable clearable>
          <el-option v-for="item in distIdList" :key="item.index" :label="item.name" :value="item.index">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('前/后')" prop="position">
        <el-select v-model="record.position" ref="position" :placeholder="$t('请选择')" size='small'>
          <el-option v-for="item in positionList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addClickMove">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="delClickMove">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { movePolicy, getPolicyArray } from '@/api/safe/access/strategy'
import { selectClass } from '@/utils/input-class.js'
export default {
  name: 'moveDialog',
  data () {
    var validateDistId = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请选择目的策略')))
      } else {
        callback()
      }
    }
    var validatePosition = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请选择目的位置')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        index: '',
        dist_id: '',
        position: 'before'
      },
      recordCopy: {
        index: '',
        dist_id: '',
        position: 'before'
      },
      dataList: [],
      positionList: [{ label: this.$t('前'), value: 'before' }, { label: this.$t('后'), value: 'after' }],
      distIdList: [],
      rules: {
        dist_id: [{ required: false, validator: validateDistId, trigger: 'change' }],
        position: [{ required: false, validator: validatePosition, trigger: 'change' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.record.index = data.index
      this.dataList.push(data)
      this.$nextTick(() => {
        const selectData = ['index', 'distId', 'position']
        selectClass(this.$refs, selectData)
      })
      getPolicyArray().then(res => {
        if (res.status === 0) {
          this.distIdList = res.info
          for (let item in this.distIdList) {
            if (data.index === this.distIdList[item].index) {
              this.distIdList.splice(item, 1)
            }
          }
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          movePolicy(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('移动成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.dataList = []
      this.distIdList = []
      this.$refs.form.resetFields()
    }
  }
}
</script>
